package com.heer.gymnasium.module.evaluate.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.module.evaluate.beans.EvaTagBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/3/1
 */

public class EvaStoreActivity extends BaseActivity implements TagFlowLayout.OnTagClickListener {
    private static final int RES_ID_LAYOUT = R.layout.layout_eva_store;
    private static final int RES_ID_GRIDVIEW = R.id.layout_eva_store_tag;
    private static final int RES_ID_RATINGBAR = R.id.layout_eva_store_ratingbar;
    private static final int RES_ID_SUBMIT = R.id.layout_eva_store_submit_tv;

    private static final int RES_ID_ITEM = R.layout.item_textview;
    private static final int RES_ID_ITEM_TV = R.id.textview;

    @BindView(RES_ID_RATINGBAR)
    RatingBar ratingBar;
    @BindView(RES_ID_GRIDVIEW)
    TagFlowLayout tagFlowLayout;

    private TagAdapter<EvaTagBean> adapter;
    private List<EvaTagBean> data;
    private Activity $this;
    private String evaType;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
    }

    @Override
    protected void initView() {
        super.initView();
        data = new ArrayList<>();
        evaType = getIntent().getStringExtra(EXTRA_TYPE);
        adapter = new TagAdapter<EvaTagBean>(data) {
            @Override
            public View getView(FlowLayout flowLayout, int i, EvaTagBean evaTagBean) {
//                View view = LayoutInflater.from($this).inflate(RES_ID_ITEM, null);
//                if (item.isSelect()) {
//                    holder.setBg(RES_ID_ITEM_TV, R.drawable.yellow_bg);
//                } else {
//                    holder.setBg(RES_ID_ITEM_TV, R.drawable.yellow_border);
//                }
//                holder.setText(RES_ID_ITEM_TV, item.getName());
//                holder.getParentView().setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        if (data.get(pos).isSelect()) {
//                            data.get(pos).setSelect(false);
//                        } else {
//                            data.get(pos).setSelect(true);
//                        }
//                        adapter.notifyDataSetChanged();
//                    }
//                });
//                return view;
                TextView textView = new TextView($this);
                textView.setText(evaTagBean.getName());
                if (evaTagBean.isSelect()) {
                    textView.setTextColor(getResources().getColor(R.color.white));
                    textView.setBackgroundResource(R.drawable.yellow_button_small);
                } else {
                    textView.setTextColor(getResources().getColor(R.color.font_gray));
                    textView.setBackgroundResource(R.drawable.yellow_border);
                }
                return textView;
            }


        };

//        myGridView.setAdapter(adapter);
        tagFlowLayout.setAdapter(adapter);
        tagFlowLayout.setOnTagClickListener(this);
        getTagList();
        if (evaType.equals("5") || evaType.equals("1") || evaType.equals("0")) {
            ratingBar.setVisibility(View.VISIBLE);
        }

    }

    @OnClick(RES_ID_SUBMIT)
    public void onClick() {
        StringBuffer tags = new StringBuffer();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelect()) {
                tags.append(data.get(i).getId() + ",");
            }
        }
        if (TextUtils.isEmpty(tags)) {
            ToastUtil.show(this, "请选择评价内容");
            return;
        }
        tags.delete(tags.length() - 1, tags.length());
        String id = getIntent().getStringExtra(EXTRA_ID);

        if (TextUtils.isEmpty(id) || TextUtils.isEmpty(evaType)) {
            ToastUtil.show(this, "订单详情ID为空");
            return;
        }
        if (evaType.equals("4") || evaType.equals("2")) {
            evalUser(id, tags.toString());
        } else if (evaType.equals("3")) {
            evalCorps(id, tags.toString());
        } else if (evaType.equals("0")) {
            evalCoach(id, tags.toString());
        } else {
            evalStadium(id, tags.toString());
        }
    }

    //评价店铺
    private void evalStadium(String id, String tags) {
        String order_id = "", detial_id = "";
        if (evaType.equals("5")) {
            order_id = id;
        } else {
            detial_id = id;
        }
        mAppHelper.apiService().evalStadium(detial_id, order_id, (int) ratingBar.getRating() + "", tags.toString()).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                ToastUtil.show($this, "评价成功");
                finish();
            }
        });
    }

    //获取标签 0:教练，1：场馆,2:用户,3:战队
    private void getTagList() {
        String evaType = getIntent().getStringExtra(EXTRA_TYPE);
        if (TextUtils.isEmpty(evaType)) {
            return;
        }
        if (evaType.equals("4")) {
            evaType = "2";
        } else if (evaType.equals("5")) {
            evaType = "1";
        }
        mAppHelper.apiService().getTagList(evaType).enqueue(new BaseCallback<ApiResponse<List<EvaTagBean>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<EvaTagBean>>> call, Response<ApiResponse<List<EvaTagBean>>> response) {
                if (response.body().getData() == null || response.body().getData().size() == 0) {
                    ToastUtil.show($this, "暂无评价标签");
                    finish();
                }
                data.addAll(response.body().getData());
                adapter.notifyDataChanged();
            }
        });
    }

    // 评价用户
    private void evalUser(String id, String tags) {
        String order_id = getIntent().getStringExtra(EXTRA_DATA);
        mAppHelper.apiService().evalUser(id, order_id, tags).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                ToastUtil.show($this, "评价成功");
                finish();
            }
        });
    }

    //评价战队
    private void evalCorps(String id, String tags) {
        mAppHelper.apiService().evalCorps(id, tags).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                ToastUtil.show($this, "评价成功");
                finish();
            }
        });
    }

    //评价教练
    private void evalCoach(String id, String tags) {
        mAppHelper.apiService().evalCoach(id, (int) ratingBar.getRating() + "", tags).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                ToastUtil.show($this, "评价成功");
                finish();
            }
        });
    }

    //type 0:教练，1：场馆,2:用户,3:战队,4:评价成员 ,5:评价店铺
    public static void startActivity(Context context, String detail_id, String type, int orderStatus) {
        Intent intent = new Intent(context, EvaStoreActivity.class);
        intent.putExtra(EXTRA_ID, detail_id);
        intent.putExtra(EXTRA_TYPE, type);
        intent.putExtra(EXTRA_FROM, orderStatus);
        context.startActivity(intent);
    }

    //评价成员
    public static void startActivity(Context context, String detail_id, String order_id, String type) {
        Intent intent = new Intent(context, EvaStoreActivity.class);
        intent.putExtra(EXTRA_ID, detail_id);
        intent.putExtra(EXTRA_DATA, order_id);
        intent.putExtra(EXTRA_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    public boolean onTagClick(View view, int position, FlowLayout flowLayout) {
        if (data.get(position).isSelect()) {
            data.get(position).setSelect(false);
        } else {
            data.get(position).setSelect(true);
        }
        adapter.notifyDataChanged();
        return false;
    }
}
